package cn.iocoder.yudao.module.scrm.service.operaterecord;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.scrm.dal.dataobject.clue.ClueDO;
import cn.iocoder.yudao.module.scrm.dal.mysql.clue.ClueMapper;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.scrm.controller.admin.operaterecord.vo.*;
import cn.iocoder.yudao.module.scrm.dal.dataobject.operaterecord.OperateRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.scrm.dal.mysql.operaterecord.OperateRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.scrm.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.module.scrm.enums.common.OperateTypeEnum.CLUE;
import static cn.iocoder.yudao.module.scrm.enums.common.OperateWayEnum.FOLLOW_UP;

/**
 * 操作记录 Service 实现类
 *
 * @author 黎志民
 */
@Service
@Validated
public class OperateRecordServiceImpl implements OperateRecordService {

    @Resource
    private OperateRecordMapper operateRecordMapper;

    @Resource
    private AdminUserApi adminUserApi;

    @Resource
    private DeptApi deptApi;

    @Resource
    private ClueMapper clueMapper;



    @Override
    public Integer createOperateRecord(OperateRecordSaveReqVO createReqVO) {
        // 获得系统用户数据
        Long loginUserId = getLoginUserId();
        AdminUserRespDTO userRespDTO = adminUserApi.getUser(loginUserId).getCheckedData();
        //DeptRespDTO deptRespDTO = deptApi.getDept(userRespDTO.getDeptId()).getCheckedData();

        // 插入
        OperateRecordDO operateRecord = BeanUtils.toBean(createReqVO, OperateRecordDO.class);
        operateRecord.setNickName(userRespDTO.getNickname());
        operateRecordMapper.insert(operateRecord);

        //设置线索中的最后跟进时间
        if (operateRecord.getType().equals(CLUE.getType()) && operateRecord.getTargetId() != null &&
        operateRecord.getWay().equals(FOLLOW_UP.getWay())){
            ClueDO clueDO = clueMapper.selectOne(new LambdaQueryWrapperX<ClueDO>()
                    .in(ClueDO::getId, operateRecord.getTargetId()));
            clueDO.setFollowUpTime(operateRecord.getFollowUpTime());
            clueMapper.updateById(clueDO);
        }

        // 返回
        return operateRecord.getId();
    }

    @Override
    public void updateOperateRecord(OperateRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateOperateRecordExists(updateReqVO.getId());
        // 更新
        OperateRecordDO updateObj = BeanUtils.toBean(updateReqVO, OperateRecordDO.class);
        operateRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteOperateRecord(Integer id) {
        // 校验存在
        validateOperateRecordExists(id);
        // 删除
        operateRecordMapper.deleteById(id);
    }

    private void validateOperateRecordExists(Integer id) {
        if (operateRecordMapper.selectById(id) == null) {
            throw exception(OPERATE_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public OperateRecordDO getOperateRecord(Integer id) {
        OperateRecordDO operateRecordDO = operateRecordMapper.selectById(id);
        return operateRecordMapper.selectById(operateRecordDO);
    }

    @Override
    public PageResult<OperateRecordDO> getOperateRecordPage(OperateRecordPageReqVO pageReqVO) {
        return operateRecordMapper.selectPage(pageReqVO);
    }

}